home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / StyleReader.java < prev    next >
Text File  |  1998-06-30  |  8KB  |  284 lines

  1. /*
  2.  * @(#)StyleReader.java    1.19 98/03/13
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20. package com.sun.java.swing.text.html;
  21.  
  22. import java.awt.Color;
  23. import java.util.Enumeration;
  24. import java.util.Vector;
  25. import java.net.URL;
  26. import java.net.MalformedURLException;
  27. import java.io.InputStream;
  28. import java.io.*;
  29.  
  30. import com.sun.java.swing.text.*;
  31.  
  32. /**
  33.  * A reader to load a Cascading Style Sheet file into a StyleContext
  34.  *
  35.  * @author  Jill Nakata
  36.  * @author  Makarand Gokhale
  37.  * @author  Sara Swanson
  38.  * @version 1.19 03/13/98
  39.  * 
  40.  */
  41. class StyleReader extends HTMLStyleCallbackDefault {
  42.  
  43.     public static final String DEFAULT_STYLE_HIERARCHY = "default-hierarchy";
  44.     public static final String CHILD_STYLE = "child-style";
  45.  
  46.     private style32 styleParser;
  47.     private StyleContext styleContext;
  48.     private Style _newStyle, _parentStyle;
  49.     private String _name;
  50.     private Vector tags;
  51.     protected static StyleSheet ss = new StyleSheet();
  52.  
  53.     public StyleReader(StyleContext sc) {
  54.     this.styleContext = sc;
  55.  
  56.     // Create the default style hierarchy
  57.         Style shStyle = styleContext.addStyle(DEFAULT_STYLE_HIERARCHY, null);
  58.     // The default style hierarchy should reference the default style
  59.         shStyle.addAttribute(CHILD_STYLE,
  60.         styleContext.getStyle(StyleContext.DEFAULT_STYLE));
  61.  
  62.     ss = new StyleSheet(styleContext.getStyle(DEFAULT_STYLE_HIERARCHY));
  63.  
  64.     tags = new Vector();
  65.     _parentStyle = styleContext.getStyle(DEFAULT_STYLE_HIERARCHY);
  66.     }
  67.  
  68.     /**
  69.      * Load the given input stream (which is expected to 
  70.      * contain valid style sheet data) into the given StyleContext.
  71.      */
  72.     public int read(StyleContext sc, int offset, InputStream is) {
  73.  
  74.     // Create the Style Parser and set Actions.
  75.         styleParser = new style32(new InputStreamReader(is));
  76.         styleParser.setCallback(this);
  77.  
  78.         try {
  79.         // Parse it.
  80.             styleParser.extstylesheet();
  81.         } catch (ParseException e) {
  82.             System.out.println("Style Parse Error");
  83.             System.out.println(e);
  84.         }
  85.  
  86.     //dumpStyles(styleContext.getStyle(DEFAULT_STYLE_HIERARCHY),"");
  87.  
  88.     return 0;
  89.     }
  90.  
  91.     /**
  92.      * Returns the style sheet.
  93.      */
  94.     public static StyleSheet getStyleSheet() {
  95.     return ss;
  96.     }
  97.  
  98.     private String makePName(String styleName) {
  99.  
  100.         String pName = null;
  101.  
  102.     if (styleName.equals("p"))
  103.       pName = Constants.IMPLIEDP;
  104.     else {
  105.       pName = styleName.substring(0,styleName.length()-1) 
  106.         + Constants.IMPLIEDP;
  107.     }
  108.  
  109.     return pName;
  110.     }
  111.  
  112.     private void dumpStyles(Style s, String indent) {
  113.         for (Enumeration e = s.getAttributeNames();
  114.             e.hasMoreElements() ;) {
  115.             Object key = e.nextElement();
  116.             Object value = s.getAttribute(key);
  117.         if (!key.equals(StyleConstants.ResolveAttribute))
  118.                 System.out.println(indent + key.toString() + " = " + value.toString());
  119.  
  120.             if((value instanceof Style)
  121.         && (! key.equals(StyleConstants.ResolveAttribute))) {
  122.  
  123.  
  124.                 dumpStyles((Style)value, indent + "  ");
  125.             }
  126.         }
  127.     }
  128.  
  129.     /************ Callbacks from Style Parser *************/
  130.  
  131.     /**
  132.      * Callback from Style Parser when a tag has been consumed.
  133.      */ 
  134.     public void tagAction(String tag) {
  135.  
  136.     //
  137.     // Set the parent style
  138.     // If body, then use default style.
  139.     //
  140.     if (tag.equals(Constants.BODY)) {
  141.         _parentStyle = styleContext.getStyle(DEFAULT_STYLE_HIERARCHY);
  142.     }
  143.  
  144.     // 
  145.     // Build up the list of tags.
  146.     //
  147.     tags.addElement(tag);
  148.  
  149.     // Make the style name string "li li p"
  150.     String styleName = makeStyleName();
  151.  
  152.       //
  153.     // Add this style and set the tags as an attribute.
  154.         // on the style.
  155.     Style childStyle = null;
  156.     if (!tag.equals(Constants.BODY)) {
  157.         // Don't duplicate any styles so try to look it up first
  158.         _newStyle = styleContext.getStyle("SH" + styleName);
  159.         if (_newStyle == null) {
  160.         // Add a new node in the style hierarchy
  161.             _newStyle =  styleContext.addStyle("SH" + styleName, null);
  162.  
  163.         // Get the childstyle from the parent so that the correct
  164.         // resolve parent can be set when childStyle is created
  165.             Style parentChildStyle 
  166.            = (Style)_parentStyle.getAttribute(CHILD_STYLE);
  167.  
  168.         // Create the childStyle that matches the _newStyle node
  169.         // in the hierarchy.
  170.             childStyle 
  171.            = styleContext.addStyle(styleName, parentChildStyle);
  172.  
  173.         // Add a pointer from the style hierarchy node to the
  174.         // childStyle and the parent style hierarchy node.
  175.             _newStyle.addAttribute(CHILD_STYLE, childStyle);
  176.             _newStyle.addAttribute(StyleConstants.ResolveAttribute, _parentStyle);
  177.  
  178.         // Make the parent node point to the new child node
  179.             _parentStyle.addAttribute("SH" + tag, _newStyle);
  180.         }
  181.  
  182. /*
  183.              // Clone the P Style into an impliedp style.
  184.         // Only clone tags ending with "p"
  185.         if (styleName.equals("p") || styleName.endsWith(" p")) {
  186.             String pName = makePName("SH" + styleName);
  187.  
  188.             // Don't duplicate any styles so try to look it up first
  189.             Style impliedStyle = styleContext.getStyle(pName);
  190.             if (impliedStyle == null) {
  191.             // Add a new node in the hierarchy for the implied style
  192.                 impliedStyle = styleContext.addStyle(pName, null);
  193.  
  194.             // Add a pointer from the style hierarchy node to the
  195.             // childStyle and the parent style hierarchy node.
  196.                 impliedStyle.addAttribute(CHILD_STYLE, childStyle);
  197.                 impliedStyle.addAttribute(StyleConstants.ResolveAttribute, _parentStyle);
  198.  
  199.             // Make the parent node point to the new child node
  200.                 _parentStyle.addAttribute(Constants.IMPLIEDP, impliedStyle);
  201.             }
  202.         }
  203. */
  204.     }
  205.     else {
  206.         _newStyle = _parentStyle;
  207.     }
  208.  
  209.         //
  210.         // Set new _parentStyle for any dep tags.
  211.     //
  212.     _parentStyle = _newStyle;
  213.     }
  214.  
  215.     /**
  216.      * Callback from Style Parser when a dependent tag has been consumed.
  217.      */ 
  218.     public void depTagAction() {
  219.         ;
  220.     }
  221.  
  222.     /**
  223.      * Callback from Style Parser when a class has been consumed.
  224.      */ 
  225.     public void classAction(String regclass) {
  226.      ;
  227.     }
  228.  
  229.     /**
  230.      * Callback from Style Parser when a pseudo class has been consumed.
  231.      */ 
  232.     public void pseudoclassAction(String pseudoclass) {
  233.      ;
  234.     }
  235.  
  236.     /**
  237.      * Callback from Style Parser when a property has been consumed.
  238.      */ 
  239.     public void propertyAction(String property) {
  240.     _name = property;
  241.     }
  242.  
  243.     /**
  244.      * Callback from Style Parser when a value for a property 
  245.      * has been consumed.
  246.      * This needs to be fixed to handle multiple values:
  247.      *      color: red, green, blue
  248.      */ 
  249.     public void valueAction(String value) {
  250.     Style childStyle 
  251.         = (Style)_newStyle.getAttribute(CHILD_STYLE);
  252.  
  253.         StyleXlater.translateProperty(ss, childStyle, _name, value);
  254.  
  255.     //
  256.     // Reset tags list and reset parent style to default style.
  257.     // Doing this for every property value for now.
  258.     //
  259.     tags.removeAllElements();
  260.     _parentStyle = styleContext.getStyle(DEFAULT_STYLE_HIERARCHY);
  261.     }
  262.  
  263.     /**
  264.      * Build a style name of current tags: "li li li p"
  265.      */ 
  266.     private String makeStyleName() {
  267.  
  268.     int length = tags.size();
  269.     String styleName = new String();
  270.  
  271.     String tag = (String)tags.elementAt(0);
  272.     styleName = styleName + tag;
  273.  
  274.     for (int i = 1; i < length; i++) {
  275.       tag = (String)tags.elementAt(i);
  276.       styleName = styleName + " " + tag;
  277.         }
  278.     return styleName;
  279.  
  280.     }
  281.  
  282.   
  283. }
  284.